<template>
  <el-card class="work">
    <p style="font-size: 24px">工作日历</p>
    <el-row type="flex" justify="end">
      <el-select v-model="currentYear" size="small" style="width: 100px" @change="getCurrent">
        <el-option v-for="item of yearList" :key="item" :label="item" :value="item" />
      </el-select>
      <el-select v-model="currentMonth" size="small" style="width: 100px;margin-left: 10px;" @change="getCurrent">
        <el-option v-for="item of 12" :key="item" :label="item" :value="item" />
      </el-select>
    </el-row>
    <el-calendar v-model="currentDate">
      <template
        slot="dateCell"
        slot-scope="{date}"
      >
        <span class="date">{{ date | getDay }}</span>
        <span v-if="getWeek(date)" class="rest">休</span>
      </template>
    </el-calendar>
  </el-card>
</template>

<script>
export default {
  name: 'WorkCalendar',
  filters: {
    getDay(value) {
      return value.getDate()
    }
  },
  props: {
    startDate: {
      type: Date,
      default: () => new Date()
    }
  },
  data() {
    return {
      yearList: [],
      currentYear: null,
      currentMonth: null,
      currentDate: null
    }
  },
  created() {
    this.getMonth()
    this.currentYear = this.startDate.getFullYear()
    this.currentMonth = this.startDate.getMonth() + 1
    this.currentDate = new Date(`${this.currentYear}-${this.currentMonth}-1`)
  },
  methods: {
    // 获取月份的前后五个年份
    getMonth() {
      this.yearList = Array.from(Array(11), (ele, index) => this.startDate.getFullYear() + index - 5)
    },
    getCurrent() {
      this.currentDate = new Date(`${this.currentYear}-${this.currentMonth}-1`)
    },
    // 获取星期
    getWeek(value) {
      return value.getDay() === 0 || value.getDay() === 6
    }
  }
}
</script>

<style scoped >
::v-deep .el-calendar .el-calendar__header {
      background: pink;
      display: none;
    }
::v-deep .el-calendar__body{
      font-size: 16px;
}
::v-deep .el-calendar__body .el-calendar-table__row td{
    border: none;
    text-align: center;
    height: 50px;
    line-height: 50px;
    font-size: 14px;
}
.date{
  display: inline-block;
    width: 22px;
    height: 22px;
}

::v-deep .el-calendar__body .el-calendar-table__row td.is-selected .date{
    background-color: #409eff;
    text-align: center;
    line-height: 20px;
    border-radius: 50%;
    color: #fff;
}
::v-deep .el-calendar__body .el-calendar-table .el-calendar-day {
  height: 50px;
  padding: unset;
}
.rest{
  display: inline-block;
  width: 18px;
  height: 18px;
  text-align: center;
  line-height: 18px;
  font-size: 12px;
  color: #fff;
  border-radius: 50%;
  background-color: #fa7c4d;
  margin-left: 10px;
}
</style>
